package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    /**
     * 添加会员
     */
    @Override
    public Member creatMember(Member member) {

        Member pojo = memberDao.selectByPhoneNumber(member.getPhoneNumber());

        if (pojo != null) {
            member.setId(pojo.getId());
            memberDao.updateByIdSelective(member);
        }else {
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        return memberDao.selectById(member.getId());
    }
    /**
     * 会员统计
     *
     * @param list
     * @return
     */
    @Override
    public List<Integer> getMemberReport(List<String> list) {
        //用于存储会员数量
        List<Integer> countList = new ArrayList<>();
        for (String mouth : list) {
            //查询这个月月底前所有会员数量


            if (mouth.contains(".01") || mouth.contains(".03") || mouth.contains(".05") || mouth.contains(".07") || mouth.contains(".08") || mouth.contains(".10") || mouth.contains(".12")) {
                //加31天
                mouth = mouth + ".31";
            }else if(mouth.contains(".02")){
                mouth =mouth+".28";
            }else {
                mouth =mouth+".30";
            }
            Integer count = this.memberDao.getMemberCount(mouth);
            countList.add(count);
        }


        return countList;
    }

    /**
     * 根据ID进行查询
     *
     * @param memberId
     * @return
     */
    @Override
    public Member findById(Integer memberId) {
        return memberDao.selectById(memberId);
    }
}
